package qldc.pm.qlktcsdldc.service.persistence;

import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.CharPool;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.UnmodifiableList;
import com.liferay.portal.model.CacheModel;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import qldc.pm.qlktcsdldc.NoSuchDanhMucSoBanNganhException;
import qldc.pm.qlktcsdldc.model.DanhMucSoBanNganh;
import qldc.pm.qlktcsdldc.model.impl.DanhMucSoBanNganhImpl;
import qldc.pm.qlktcsdldc.model.impl.DanhMucSoBanNganhModelImpl;
import qldc.pm.qlktcsdldc.service.persistence.DanhMucSoBanNganhPersistence;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the danh muc so ban nganh service.
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Brian Wing Shun Chan
 * @see DanhMucSoBanNganhPersistence
 * @see DanhMucSoBanNganhUtil
 * @generated
 */
public class DanhMucSoBanNganhPersistenceImpl extends BasePersistenceImpl<DanhMucSoBanNganh>
    implements DanhMucSoBanNganhPersistence {
    /*
     * NOTE FOR DEVELOPERS:
     *
     * Never modify or reference this class directly. Always use {@link DanhMucSoBanNganhUtil} to access the danh muc so ban nganh persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
     */
    public static final String FINDER_CLASS_NAME_ENTITY = DanhMucSoBanNganhImpl.class.getName();
    public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
        ".List1";
    public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
        ".List2";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(DanhMucSoBanNganhModelImpl.ENTITY_CACHE_ENABLED,
            DanhMucSoBanNganhModelImpl.FINDER_CACHE_ENABLED,
            DanhMucSoBanNganhImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(DanhMucSoBanNganhModelImpl.ENTITY_CACHE_ENABLED,
            DanhMucSoBanNganhModelImpl.FINDER_CACHE_ENABLED,
            DanhMucSoBanNganhImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(DanhMucSoBanNganhModelImpl.ENTITY_CACHE_ENABLED,
            DanhMucSoBanNganhModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_IDQLKT_DANHMUCSOBANNGANH =
        new FinderPath(DanhMucSoBanNganhModelImpl.ENTITY_CACHE_ENABLED,
            DanhMucSoBanNganhModelImpl.FINDER_CACHE_ENABLED,
            DanhMucSoBanNganhImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
            "findByIDQLKT_DanhMucSoBanNganh",
            new String[] {
                Long.class.getName(),
                
            Integer.class.getName(), Integer.class.getName(),
                OrderByComparator.class.getName()
            });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_IDQLKT_DANHMUCSOBANNGANH =
        new FinderPath(DanhMucSoBanNganhModelImpl.ENTITY_CACHE_ENABLED,
            DanhMucSoBanNganhModelImpl.FINDER_CACHE_ENABLED,
            DanhMucSoBanNganhImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "findByIDQLKT_DanhMucSoBanNganh",
            new String[] { Long.class.getName() },
            DanhMucSoBanNganhModelImpl.IDQLKT_DANHMUCSOBANNGANH_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_IDQLKT_DANHMUCSOBANNGANH =
        new FinderPath(DanhMucSoBanNganhModelImpl.ENTITY_CACHE_ENABLED,
            DanhMucSoBanNganhModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "countByIDQLKT_DanhMucSoBanNganh",
            new String[] { Long.class.getName() });
    private static final String _FINDER_COLUMN_IDQLKT_DANHMUCSOBANNGANH_IDQLKT_DANHMUCSOBANNGANH_2 =
        "danhMucSoBanNganh.IDQLKT_DanhMucSoBanNganh = ?";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_TENSOBANNGANH =
        new FinderPath(DanhMucSoBanNganhModelImpl.ENTITY_CACHE_ENABLED,
            DanhMucSoBanNganhModelImpl.FINDER_CACHE_ENABLED,
            DanhMucSoBanNganhImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByTenSoBanNganh",
            new String[] {
                String.class.getName(),
                
            Integer.class.getName(), Integer.class.getName(),
                OrderByComparator.class.getName()
            });
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_COUNT_BY_TENSOBANNGANH =
        new FinderPath(DanhMucSoBanNganhModelImpl.ENTITY_CACHE_ENABLED,
            DanhMucSoBanNganhModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "countByTenSoBanNganh",
            new String[] { String.class.getName() });
    private static final String _FINDER_COLUMN_TENSOBANNGANH_TENSOBANNGANH_1 = "danhMucSoBanNganh.TenSoBanNganh LIKE NULL";
    private static final String _FINDER_COLUMN_TENSOBANNGANH_TENSOBANNGANH_2 = "danhMucSoBanNganh.TenSoBanNganh LIKE ?";
    private static final String _FINDER_COLUMN_TENSOBANNGANH_TENSOBANNGANH_3 = "(danhMucSoBanNganh.TenSoBanNganh IS NULL OR danhMucSoBanNganh.TenSoBanNganh LIKE '')";
    private static final String _SQL_SELECT_DANHMUCSOBANNGANH = "SELECT danhMucSoBanNganh FROM DanhMucSoBanNganh danhMucSoBanNganh";
    private static final String _SQL_SELECT_DANHMUCSOBANNGANH_WHERE = "SELECT danhMucSoBanNganh FROM DanhMucSoBanNganh danhMucSoBanNganh WHERE ";
    private static final String _SQL_COUNT_DANHMUCSOBANNGANH = "SELECT COUNT(danhMucSoBanNganh) FROM DanhMucSoBanNganh danhMucSoBanNganh";
    private static final String _SQL_COUNT_DANHMUCSOBANNGANH_WHERE = "SELECT COUNT(danhMucSoBanNganh) FROM DanhMucSoBanNganh danhMucSoBanNganh WHERE ";
    private static final String _ORDER_BY_ENTITY_ALIAS = "danhMucSoBanNganh.";
    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No DanhMucSoBanNganh exists with the primary key ";
    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No DanhMucSoBanNganh exists with the key {";
    private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = GetterUtil.getBoolean(PropsUtil.get(
                PropsKeys.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE));
    private static Log _log = LogFactoryUtil.getLog(DanhMucSoBanNganhPersistenceImpl.class);
    private static DanhMucSoBanNganh _nullDanhMucSoBanNganh = new DanhMucSoBanNganhImpl() {
            @Override
            public Object clone() {
                return this;
            }

            @Override
            public CacheModel<DanhMucSoBanNganh> toCacheModel() {
                return _nullDanhMucSoBanNganhCacheModel;
            }
        };

    private static CacheModel<DanhMucSoBanNganh> _nullDanhMucSoBanNganhCacheModel =
        new CacheModel<DanhMucSoBanNganh>() {
            @Override
            public DanhMucSoBanNganh toEntityModel() {
                return _nullDanhMucSoBanNganh;
            }
        };

    public DanhMucSoBanNganhPersistenceImpl() {
        setModelClass(DanhMucSoBanNganh.class);
    }

    /**
     * Returns all the danh muc so ban nganhs where IDQLKT_DanhMucSoBanNganh = &#63;.
     *
     * @param IDQLKT_DanhMucSoBanNganh the i d q l k t_ danh muc so ban nganh
     * @return the matching danh muc so ban nganhs
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<DanhMucSoBanNganh> findByIDQLKT_DanhMucSoBanNganh(
        long IDQLKT_DanhMucSoBanNganh) throws SystemException {
        return findByIDQLKT_DanhMucSoBanNganh(IDQLKT_DanhMucSoBanNganh,
            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the danh muc so ban nganhs where IDQLKT_DanhMucSoBanNganh = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.qlktcsdldc.model.impl.DanhMucSoBanNganhModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param IDQLKT_DanhMucSoBanNganh the i d q l k t_ danh muc so ban nganh
     * @param start the lower bound of the range of danh muc so ban nganhs
     * @param end the upper bound of the range of danh muc so ban nganhs (not inclusive)
     * @return the range of matching danh muc so ban nganhs
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<DanhMucSoBanNganh> findByIDQLKT_DanhMucSoBanNganh(
        long IDQLKT_DanhMucSoBanNganh, int start, int end)
        throws SystemException {
        return findByIDQLKT_DanhMucSoBanNganh(IDQLKT_DanhMucSoBanNganh, start,
            end, null);
    }

    /**
     * Returns an ordered range of all the danh muc so ban nganhs where IDQLKT_DanhMucSoBanNganh = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.qlktcsdldc.model.impl.DanhMucSoBanNganhModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param IDQLKT_DanhMucSoBanNganh the i d q l k t_ danh muc so ban nganh
     * @param start the lower bound of the range of danh muc so ban nganhs
     * @param end the upper bound of the range of danh muc so ban nganhs (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching danh muc so ban nganhs
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<DanhMucSoBanNganh> findByIDQLKT_DanhMucSoBanNganh(
        long IDQLKT_DanhMucSoBanNganh, int start, int end,
        OrderByComparator orderByComparator) throws SystemException {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
                (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_IDQLKT_DANHMUCSOBANNGANH;
            finderArgs = new Object[] { IDQLKT_DanhMucSoBanNganh };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_IDQLKT_DANHMUCSOBANNGANH;
            finderArgs = new Object[] {
                    IDQLKT_DanhMucSoBanNganh,
                    
                    start, end, orderByComparator
                };
        }

        List<DanhMucSoBanNganh> list = (List<DanhMucSoBanNganh>) FinderCacheUtil.getResult(finderPath,
                finderArgs, this);

        if ((list != null) && !list.isEmpty()) {
            for (DanhMucSoBanNganh danhMucSoBanNganh : list) {
                if ((IDQLKT_DanhMucSoBanNganh != danhMucSoBanNganh.getIDQLKT_DanhMucSoBanNganh())) {
                    list = null;

                    break;
                }
            }
        }

        if (list == null) {
            StringBundler query = null;

            if (orderByComparator != null) {
                query = new StringBundler(3 +
                        (orderByComparator.getOrderByFields().length * 3));
            } else {
                query = new StringBundler(3);
            }

            query.append(_SQL_SELECT_DANHMUCSOBANNGANH_WHERE);

            query.append(_FINDER_COLUMN_IDQLKT_DANHMUCSOBANNGANH_IDQLKT_DANHMUCSOBANNGANH_2);

            if (orderByComparator != null) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
                    orderByComparator);
            } else
             if (pagination) {
                query.append(DanhMucSoBanNganhModelImpl.ORDER_BY_JPQL);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(IDQLKT_DanhMucSoBanNganh);

                if (!pagination) {
                    list = (List<DanhMucSoBanNganh>) QueryUtil.list(q,
                            getDialect(), start, end, false);

                    Collections.sort(list);

                    list = new UnmodifiableList<DanhMucSoBanNganh>(list);
                } else {
                    list = (List<DanhMucSoBanNganh>) QueryUtil.list(q,
                            getDialect(), start, end);
                }

                cacheResult(list);

                FinderCacheUtil.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Returns the first danh muc so ban nganh in the ordered set where IDQLKT_DanhMucSoBanNganh = &#63;.
     *
     * @param IDQLKT_DanhMucSoBanNganh the i d q l k t_ danh muc so ban nganh
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching danh muc so ban nganh
     * @throws qldc.pm.qlktcsdldc.NoSuchDanhMucSoBanNganhException if a matching danh muc so ban nganh could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DanhMucSoBanNganh findByIDQLKT_DanhMucSoBanNganh_First(
        long IDQLKT_DanhMucSoBanNganh, OrderByComparator orderByComparator)
        throws NoSuchDanhMucSoBanNganhException, SystemException {
        DanhMucSoBanNganh danhMucSoBanNganh = fetchByIDQLKT_DanhMucSoBanNganh_First(IDQLKT_DanhMucSoBanNganh,
                orderByComparator);

        if (danhMucSoBanNganh != null) {
            return danhMucSoBanNganh;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("IDQLKT_DanhMucSoBanNganh=");
        msg.append(IDQLKT_DanhMucSoBanNganh);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchDanhMucSoBanNganhException(msg.toString());
    }

    /**
     * Returns the first danh muc so ban nganh in the ordered set where IDQLKT_DanhMucSoBanNganh = &#63;.
     *
     * @param IDQLKT_DanhMucSoBanNganh the i d q l k t_ danh muc so ban nganh
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching danh muc so ban nganh, or <code>null</code> if a matching danh muc so ban nganh could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DanhMucSoBanNganh fetchByIDQLKT_DanhMucSoBanNganh_First(
        long IDQLKT_DanhMucSoBanNganh, OrderByComparator orderByComparator)
        throws SystemException {
        List<DanhMucSoBanNganh> list = findByIDQLKT_DanhMucSoBanNganh(IDQLKT_DanhMucSoBanNganh,
                0, 1, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the last danh muc so ban nganh in the ordered set where IDQLKT_DanhMucSoBanNganh = &#63;.
     *
     * @param IDQLKT_DanhMucSoBanNganh the i d q l k t_ danh muc so ban nganh
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching danh muc so ban nganh
     * @throws qldc.pm.qlktcsdldc.NoSuchDanhMucSoBanNganhException if a matching danh muc so ban nganh could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DanhMucSoBanNganh findByIDQLKT_DanhMucSoBanNganh_Last(
        long IDQLKT_DanhMucSoBanNganh, OrderByComparator orderByComparator)
        throws NoSuchDanhMucSoBanNganhException, SystemException {
        DanhMucSoBanNganh danhMucSoBanNganh = fetchByIDQLKT_DanhMucSoBanNganh_Last(IDQLKT_DanhMucSoBanNganh,
                orderByComparator);

        if (danhMucSoBanNganh != null) {
            return danhMucSoBanNganh;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("IDQLKT_DanhMucSoBanNganh=");
        msg.append(IDQLKT_DanhMucSoBanNganh);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchDanhMucSoBanNganhException(msg.toString());
    }

    /**
     * Returns the last danh muc so ban nganh in the ordered set where IDQLKT_DanhMucSoBanNganh = &#63;.
     *
     * @param IDQLKT_DanhMucSoBanNganh the i d q l k t_ danh muc so ban nganh
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching danh muc so ban nganh, or <code>null</code> if a matching danh muc so ban nganh could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DanhMucSoBanNganh fetchByIDQLKT_DanhMucSoBanNganh_Last(
        long IDQLKT_DanhMucSoBanNganh, OrderByComparator orderByComparator)
        throws SystemException {
        int count = countByIDQLKT_DanhMucSoBanNganh(IDQLKT_DanhMucSoBanNganh);

        if (count == 0) {
            return null;
        }

        List<DanhMucSoBanNganh> list = findByIDQLKT_DanhMucSoBanNganh(IDQLKT_DanhMucSoBanNganh,
                count - 1, count, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Removes all the danh muc so ban nganhs where IDQLKT_DanhMucSoBanNganh = &#63; from the database.
     *
     * @param IDQLKT_DanhMucSoBanNganh the i d q l k t_ danh muc so ban nganh
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeByIDQLKT_DanhMucSoBanNganh(long IDQLKT_DanhMucSoBanNganh)
        throws SystemException {
        for (DanhMucSoBanNganh danhMucSoBanNganh : findByIDQLKT_DanhMucSoBanNganh(
                IDQLKT_DanhMucSoBanNganh, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
                null)) {
            remove(danhMucSoBanNganh);
        }
    }

    /**
     * Returns the number of danh muc so ban nganhs where IDQLKT_DanhMucSoBanNganh = &#63;.
     *
     * @param IDQLKT_DanhMucSoBanNganh the i d q l k t_ danh muc so ban nganh
     * @return the number of matching danh muc so ban nganhs
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countByIDQLKT_DanhMucSoBanNganh(long IDQLKT_DanhMucSoBanNganh)
        throws SystemException {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_IDQLKT_DANHMUCSOBANNGANH;

        Object[] finderArgs = new Object[] { IDQLKT_DanhMucSoBanNganh };

        Long count = (Long) FinderCacheUtil.getResult(finderPath, finderArgs,
                this);

        if (count == null) {
            StringBundler query = new StringBundler(2);

            query.append(_SQL_COUNT_DANHMUCSOBANNGANH_WHERE);

            query.append(_FINDER_COLUMN_IDQLKT_DANHMUCSOBANNGANH_IDQLKT_DANHMUCSOBANNGANH_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(IDQLKT_DanhMucSoBanNganh);

                count = (Long) q.uniqueResult();

                FinderCacheUtil.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    /**
     * Returns all the danh muc so ban nganhs where TenSoBanNganh LIKE &#63;.
     *
     * @param TenSoBanNganh the ten so ban nganh
     * @return the matching danh muc so ban nganhs
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<DanhMucSoBanNganh> findByTenSoBanNganh(String TenSoBanNganh)
        throws SystemException {
        return findByTenSoBanNganh(TenSoBanNganh, QueryUtil.ALL_POS,
            QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the danh muc so ban nganhs where TenSoBanNganh LIKE &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.qlktcsdldc.model.impl.DanhMucSoBanNganhModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param TenSoBanNganh the ten so ban nganh
     * @param start the lower bound of the range of danh muc so ban nganhs
     * @param end the upper bound of the range of danh muc so ban nganhs (not inclusive)
     * @return the range of matching danh muc so ban nganhs
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<DanhMucSoBanNganh> findByTenSoBanNganh(String TenSoBanNganh,
        int start, int end) throws SystemException {
        return findByTenSoBanNganh(TenSoBanNganh, start, end, null);
    }

    /**
     * Returns an ordered range of all the danh muc so ban nganhs where TenSoBanNganh LIKE &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.qlktcsdldc.model.impl.DanhMucSoBanNganhModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param TenSoBanNganh the ten so ban nganh
     * @param start the lower bound of the range of danh muc so ban nganhs
     * @param end the upper bound of the range of danh muc so ban nganhs (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching danh muc so ban nganhs
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<DanhMucSoBanNganh> findByTenSoBanNganh(String TenSoBanNganh,
        int start, int end, OrderByComparator orderByComparator)
        throws SystemException {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_TENSOBANNGANH;
        finderArgs = new Object[] { TenSoBanNganh, start, end, orderByComparator };

        List<DanhMucSoBanNganh> list = (List<DanhMucSoBanNganh>) FinderCacheUtil.getResult(finderPath,
                finderArgs, this);

        if ((list != null) && !list.isEmpty()) {
            for (DanhMucSoBanNganh danhMucSoBanNganh : list) {
                if (!StringUtil.wildcardMatches(
                            danhMucSoBanNganh.getTenSoBanNganh(),
                            TenSoBanNganh, CharPool.UNDERLINE,
                            CharPool.PERCENT, CharPool.BACK_SLASH, true)) {
                    list = null;

                    break;
                }
            }
        }

        if (list == null) {
            StringBundler query = null;

            if (orderByComparator != null) {
                query = new StringBundler(3 +
                        (orderByComparator.getOrderByFields().length * 3));
            } else {
                query = new StringBundler(3);
            }

            query.append(_SQL_SELECT_DANHMUCSOBANNGANH_WHERE);

            boolean bindTenSoBanNganh = false;

            if (TenSoBanNganh == null) {
                query.append(_FINDER_COLUMN_TENSOBANNGANH_TENSOBANNGANH_1);
            } else if (TenSoBanNganh.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_TENSOBANNGANH_TENSOBANNGANH_3);
            } else {
                bindTenSoBanNganh = true;

                query.append(_FINDER_COLUMN_TENSOBANNGANH_TENSOBANNGANH_2);
            }

            if (orderByComparator != null) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
                    orderByComparator);
            } else
             if (pagination) {
                query.append(DanhMucSoBanNganhModelImpl.ORDER_BY_JPQL);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindTenSoBanNganh) {
                    qPos.add(TenSoBanNganh);
                }

                if (!pagination) {
                    list = (List<DanhMucSoBanNganh>) QueryUtil.list(q,
                            getDialect(), start, end, false);

                    Collections.sort(list);

                    list = new UnmodifiableList<DanhMucSoBanNganh>(list);
                } else {
                    list = (List<DanhMucSoBanNganh>) QueryUtil.list(q,
                            getDialect(), start, end);
                }

                cacheResult(list);

                FinderCacheUtil.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Returns the first danh muc so ban nganh in the ordered set where TenSoBanNganh LIKE &#63;.
     *
     * @param TenSoBanNganh the ten so ban nganh
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching danh muc so ban nganh
     * @throws qldc.pm.qlktcsdldc.NoSuchDanhMucSoBanNganhException if a matching danh muc so ban nganh could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DanhMucSoBanNganh findByTenSoBanNganh_First(String TenSoBanNganh,
        OrderByComparator orderByComparator)
        throws NoSuchDanhMucSoBanNganhException, SystemException {
        DanhMucSoBanNganh danhMucSoBanNganh = fetchByTenSoBanNganh_First(TenSoBanNganh,
                orderByComparator);

        if (danhMucSoBanNganh != null) {
            return danhMucSoBanNganh;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("TenSoBanNganh=");
        msg.append(TenSoBanNganh);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchDanhMucSoBanNganhException(msg.toString());
    }

    /**
     * Returns the first danh muc so ban nganh in the ordered set where TenSoBanNganh LIKE &#63;.
     *
     * @param TenSoBanNganh the ten so ban nganh
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching danh muc so ban nganh, or <code>null</code> if a matching danh muc so ban nganh could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DanhMucSoBanNganh fetchByTenSoBanNganh_First(String TenSoBanNganh,
        OrderByComparator orderByComparator) throws SystemException {
        List<DanhMucSoBanNganh> list = findByTenSoBanNganh(TenSoBanNganh, 0, 1,
                orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the last danh muc so ban nganh in the ordered set where TenSoBanNganh LIKE &#63;.
     *
     * @param TenSoBanNganh the ten so ban nganh
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching danh muc so ban nganh
     * @throws qldc.pm.qlktcsdldc.NoSuchDanhMucSoBanNganhException if a matching danh muc so ban nganh could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DanhMucSoBanNganh findByTenSoBanNganh_Last(String TenSoBanNganh,
        OrderByComparator orderByComparator)
        throws NoSuchDanhMucSoBanNganhException, SystemException {
        DanhMucSoBanNganh danhMucSoBanNganh = fetchByTenSoBanNganh_Last(TenSoBanNganh,
                orderByComparator);

        if (danhMucSoBanNganh != null) {
            return danhMucSoBanNganh;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("TenSoBanNganh=");
        msg.append(TenSoBanNganh);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchDanhMucSoBanNganhException(msg.toString());
    }

    /**
     * Returns the last danh muc so ban nganh in the ordered set where TenSoBanNganh LIKE &#63;.
     *
     * @param TenSoBanNganh the ten so ban nganh
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching danh muc so ban nganh, or <code>null</code> if a matching danh muc so ban nganh could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DanhMucSoBanNganh fetchByTenSoBanNganh_Last(String TenSoBanNganh,
        OrderByComparator orderByComparator) throws SystemException {
        int count = countByTenSoBanNganh(TenSoBanNganh);

        if (count == 0) {
            return null;
        }

        List<DanhMucSoBanNganh> list = findByTenSoBanNganh(TenSoBanNganh,
                count - 1, count, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the danh muc so ban nganhs before and after the current danh muc so ban nganh in the ordered set where TenSoBanNganh LIKE &#63;.
     *
     * @param IDQLKT_DanhMucSoBanNganh the primary key of the current danh muc so ban nganh
     * @param TenSoBanNganh the ten so ban nganh
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next danh muc so ban nganh
     * @throws qldc.pm.qlktcsdldc.NoSuchDanhMucSoBanNganhException if a danh muc so ban nganh with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DanhMucSoBanNganh[] findByTenSoBanNganh_PrevAndNext(
        long IDQLKT_DanhMucSoBanNganh, String TenSoBanNganh,
        OrderByComparator orderByComparator)
        throws NoSuchDanhMucSoBanNganhException, SystemException {
        DanhMucSoBanNganh danhMucSoBanNganh = findByPrimaryKey(IDQLKT_DanhMucSoBanNganh);

        Session session = null;

        try {
            session = openSession();

            DanhMucSoBanNganh[] array = new DanhMucSoBanNganhImpl[3];

            array[0] = getByTenSoBanNganh_PrevAndNext(session,
                    danhMucSoBanNganh, TenSoBanNganh, orderByComparator, true);

            array[1] = danhMucSoBanNganh;

            array[2] = getByTenSoBanNganh_PrevAndNext(session,
                    danhMucSoBanNganh, TenSoBanNganh, orderByComparator, false);

            return array;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    protected DanhMucSoBanNganh getByTenSoBanNganh_PrevAndNext(
        Session session, DanhMucSoBanNganh danhMucSoBanNganh,
        String TenSoBanNganh, OrderByComparator orderByComparator,
        boolean previous) {
        StringBundler query = null;

        if (orderByComparator != null) {
            query = new StringBundler(6 +
                    (orderByComparator.getOrderByFields().length * 6));
        } else {
            query = new StringBundler(3);
        }

        query.append(_SQL_SELECT_DANHMUCSOBANNGANH_WHERE);

        boolean bindTenSoBanNganh = false;

        if (TenSoBanNganh == null) {
            query.append(_FINDER_COLUMN_TENSOBANNGANH_TENSOBANNGANH_1);
        } else if (TenSoBanNganh.equals(StringPool.BLANK)) {
            query.append(_FINDER_COLUMN_TENSOBANNGANH_TENSOBANNGANH_3);
        } else {
            bindTenSoBanNganh = true;

            query.append(_FINDER_COLUMN_TENSOBANNGANH_TENSOBANNGANH_2);
        }

        if (orderByComparator != null) {
            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

            if (orderByConditionFields.length > 0) {
                query.append(WHERE_AND);
            }

            for (int i = 0; i < orderByConditionFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByConditionFields[i]);

                if ((i + 1) < orderByConditionFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
                    } else {
                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN);
                    } else {
                        query.append(WHERE_LESSER_THAN);
                    }
                }
            }

            query.append(ORDER_BY_CLAUSE);

            String[] orderByFields = orderByComparator.getOrderByFields();

            for (int i = 0; i < orderByFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByFields[i]);

                if ((i + 1) < orderByFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC_HAS_NEXT);
                    } else {
                        query.append(ORDER_BY_DESC_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC);
                    } else {
                        query.append(ORDER_BY_DESC);
                    }
                }
            }
        } else {
            query.append(DanhMucSoBanNganhModelImpl.ORDER_BY_JPQL);
        }

        String sql = query.toString();

        Query q = session.createQuery(sql);

        q.setFirstResult(0);
        q.setMaxResults(2);

        QueryPos qPos = QueryPos.getInstance(q);

        if (bindTenSoBanNganh) {
            qPos.add(TenSoBanNganh);
        }

        if (orderByComparator != null) {
            Object[] values = orderByComparator.getOrderByConditionValues(danhMucSoBanNganh);

            for (Object value : values) {
                qPos.add(value);
            }
        }

        List<DanhMucSoBanNganh> list = q.list();

        if (list.size() == 2) {
            return list.get(1);
        } else {
            return null;
        }
    }

    /**
     * Removes all the danh muc so ban nganhs where TenSoBanNganh LIKE &#63; from the database.
     *
     * @param TenSoBanNganh the ten so ban nganh
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeByTenSoBanNganh(String TenSoBanNganh)
        throws SystemException {
        for (DanhMucSoBanNganh danhMucSoBanNganh : findByTenSoBanNganh(
                TenSoBanNganh, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
            remove(danhMucSoBanNganh);
        }
    }

    /**
     * Returns the number of danh muc so ban nganhs where TenSoBanNganh LIKE &#63;.
     *
     * @param TenSoBanNganh the ten so ban nganh
     * @return the number of matching danh muc so ban nganhs
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countByTenSoBanNganh(String TenSoBanNganh)
        throws SystemException {
        FinderPath finderPath = FINDER_PATH_WITH_PAGINATION_COUNT_BY_TENSOBANNGANH;

        Object[] finderArgs = new Object[] { TenSoBanNganh };

        Long count = (Long) FinderCacheUtil.getResult(finderPath, finderArgs,
                this);

        if (count == null) {
            StringBundler query = new StringBundler(2);

            query.append(_SQL_COUNT_DANHMUCSOBANNGANH_WHERE);

            boolean bindTenSoBanNganh = false;

            if (TenSoBanNganh == null) {
                query.append(_FINDER_COLUMN_TENSOBANNGANH_TENSOBANNGANH_1);
            } else if (TenSoBanNganh.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_TENSOBANNGANH_TENSOBANNGANH_3);
            } else {
                bindTenSoBanNganh = true;

                query.append(_FINDER_COLUMN_TENSOBANNGANH_TENSOBANNGANH_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindTenSoBanNganh) {
                    qPos.add(TenSoBanNganh);
                }

                count = (Long) q.uniqueResult();

                FinderCacheUtil.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    /**
     * Caches the danh muc so ban nganh in the entity cache if it is enabled.
     *
     * @param danhMucSoBanNganh the danh muc so ban nganh
     */
    @Override
    public void cacheResult(DanhMucSoBanNganh danhMucSoBanNganh) {
        EntityCacheUtil.putResult(DanhMucSoBanNganhModelImpl.ENTITY_CACHE_ENABLED,
            DanhMucSoBanNganhImpl.class, danhMucSoBanNganh.getPrimaryKey(),
            danhMucSoBanNganh);

        danhMucSoBanNganh.resetOriginalValues();
    }

    /**
     * Caches the danh muc so ban nganhs in the entity cache if it is enabled.
     *
     * @param danhMucSoBanNganhs the danh muc so ban nganhs
     */
    @Override
    public void cacheResult(List<DanhMucSoBanNganh> danhMucSoBanNganhs) {
        for (DanhMucSoBanNganh danhMucSoBanNganh : danhMucSoBanNganhs) {
            if (EntityCacheUtil.getResult(
                        DanhMucSoBanNganhModelImpl.ENTITY_CACHE_ENABLED,
                        DanhMucSoBanNganhImpl.class,
                        danhMucSoBanNganh.getPrimaryKey()) == null) {
                cacheResult(danhMucSoBanNganh);
            } else {
                danhMucSoBanNganh.resetOriginalValues();
            }
        }
    }

    /**
     * Clears the cache for all danh muc so ban nganhs.
     *
     * <p>
     * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache() {
        if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
            CacheRegistryUtil.clear(DanhMucSoBanNganhImpl.class.getName());
        }

        EntityCacheUtil.clearCache(DanhMucSoBanNganhImpl.class.getName());

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    /**
     * Clears the cache for the danh muc so ban nganh.
     *
     * <p>
     * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache(DanhMucSoBanNganh danhMucSoBanNganh) {
        EntityCacheUtil.removeResult(DanhMucSoBanNganhModelImpl.ENTITY_CACHE_ENABLED,
            DanhMucSoBanNganhImpl.class, danhMucSoBanNganh.getPrimaryKey());

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    @Override
    public void clearCache(List<DanhMucSoBanNganh> danhMucSoBanNganhs) {
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

        for (DanhMucSoBanNganh danhMucSoBanNganh : danhMucSoBanNganhs) {
            EntityCacheUtil.removeResult(DanhMucSoBanNganhModelImpl.ENTITY_CACHE_ENABLED,
                DanhMucSoBanNganhImpl.class, danhMucSoBanNganh.getPrimaryKey());
        }
    }

    /**
     * Creates a new danh muc so ban nganh with the primary key. Does not add the danh muc so ban nganh to the database.
     *
     * @param IDQLKT_DanhMucSoBanNganh the primary key for the new danh muc so ban nganh
     * @return the new danh muc so ban nganh
     */
    @Override
    public DanhMucSoBanNganh create(long IDQLKT_DanhMucSoBanNganh) {
        DanhMucSoBanNganh danhMucSoBanNganh = new DanhMucSoBanNganhImpl();

        danhMucSoBanNganh.setNew(true);
        danhMucSoBanNganh.setPrimaryKey(IDQLKT_DanhMucSoBanNganh);

        return danhMucSoBanNganh;
    }

    /**
     * Removes the danh muc so ban nganh with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param IDQLKT_DanhMucSoBanNganh the primary key of the danh muc so ban nganh
     * @return the danh muc so ban nganh that was removed
     * @throws qldc.pm.qlktcsdldc.NoSuchDanhMucSoBanNganhException if a danh muc so ban nganh with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DanhMucSoBanNganh remove(long IDQLKT_DanhMucSoBanNganh)
        throws NoSuchDanhMucSoBanNganhException, SystemException {
        return remove((Serializable) IDQLKT_DanhMucSoBanNganh);
    }

    /**
     * Removes the danh muc so ban nganh with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param primaryKey the primary key of the danh muc so ban nganh
     * @return the danh muc so ban nganh that was removed
     * @throws qldc.pm.qlktcsdldc.NoSuchDanhMucSoBanNganhException if a danh muc so ban nganh with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DanhMucSoBanNganh remove(Serializable primaryKey)
        throws NoSuchDanhMucSoBanNganhException, SystemException {
        Session session = null;

        try {
            session = openSession();

            DanhMucSoBanNganh danhMucSoBanNganh = (DanhMucSoBanNganh) session.get(DanhMucSoBanNganhImpl.class,
                    primaryKey);

            if (danhMucSoBanNganh == null) {
                if (_log.isWarnEnabled()) {
                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
                }

                throw new NoSuchDanhMucSoBanNganhException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
                    primaryKey);
            }

            return remove(danhMucSoBanNganh);
        } catch (NoSuchDanhMucSoBanNganhException nsee) {
            throw nsee;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    @Override
    protected DanhMucSoBanNganh removeImpl(DanhMucSoBanNganh danhMucSoBanNganh)
        throws SystemException {
        danhMucSoBanNganh = toUnwrappedModel(danhMucSoBanNganh);

        Session session = null;

        try {
            session = openSession();

            if (!session.contains(danhMucSoBanNganh)) {
                danhMucSoBanNganh = (DanhMucSoBanNganh) session.get(DanhMucSoBanNganhImpl.class,
                        danhMucSoBanNganh.getPrimaryKeyObj());
            }

            if (danhMucSoBanNganh != null) {
                session.delete(danhMucSoBanNganh);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        if (danhMucSoBanNganh != null) {
            clearCache(danhMucSoBanNganh);
        }

        return danhMucSoBanNganh;
    }

    @Override
    public DanhMucSoBanNganh updateImpl(
        qldc.pm.qlktcsdldc.model.DanhMucSoBanNganh danhMucSoBanNganh)
        throws SystemException {
        danhMucSoBanNganh = toUnwrappedModel(danhMucSoBanNganh);

        boolean isNew = danhMucSoBanNganh.isNew();

        DanhMucSoBanNganhModelImpl danhMucSoBanNganhModelImpl = (DanhMucSoBanNganhModelImpl) danhMucSoBanNganh;

        Session session = null;

        try {
            session = openSession();

            if (danhMucSoBanNganh.isNew()) {
                session.save(danhMucSoBanNganh);

                danhMucSoBanNganh.setNew(false);
            } else {
                session.merge(danhMucSoBanNganh);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

        if (isNew || !DanhMucSoBanNganhModelImpl.COLUMN_BITMASK_ENABLED) {
            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
        }
        else {
            if ((danhMucSoBanNganhModelImpl.getColumnBitmask() &
                    FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_IDQLKT_DANHMUCSOBANNGANH.getColumnBitmask()) != 0) {
                Object[] args = new Object[] {
                        danhMucSoBanNganhModelImpl.getOriginalIDQLKT_DanhMucSoBanNganh()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_IDQLKT_DANHMUCSOBANNGANH,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_IDQLKT_DANHMUCSOBANNGANH,
                    args);

                args = new Object[] {
                        danhMucSoBanNganhModelImpl.getIDQLKT_DanhMucSoBanNganh()
                    };

                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_IDQLKT_DANHMUCSOBANNGANH,
                    args);
                FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_IDQLKT_DANHMUCSOBANNGANH,
                    args);
            }
        }

        EntityCacheUtil.putResult(DanhMucSoBanNganhModelImpl.ENTITY_CACHE_ENABLED,
            DanhMucSoBanNganhImpl.class, danhMucSoBanNganh.getPrimaryKey(),
            danhMucSoBanNganh);

        return danhMucSoBanNganh;
    }

    protected DanhMucSoBanNganh toUnwrappedModel(
        DanhMucSoBanNganh danhMucSoBanNganh) {
        if (danhMucSoBanNganh instanceof DanhMucSoBanNganhImpl) {
            return danhMucSoBanNganh;
        }

        DanhMucSoBanNganhImpl danhMucSoBanNganhImpl = new DanhMucSoBanNganhImpl();

        danhMucSoBanNganhImpl.setNew(danhMucSoBanNganh.isNew());
        danhMucSoBanNganhImpl.setPrimaryKey(danhMucSoBanNganh.getPrimaryKey());

        danhMucSoBanNganhImpl.setIDQLKT_DanhMucSoBanNganh(danhMucSoBanNganh.getIDQLKT_DanhMucSoBanNganh());
        danhMucSoBanNganhImpl.setGroupId(danhMucSoBanNganh.getGroupId());
        danhMucSoBanNganhImpl.setCompanyId(danhMucSoBanNganh.getCompanyId());
        danhMucSoBanNganhImpl.setUserId(danhMucSoBanNganh.getUserId());
        danhMucSoBanNganhImpl.setUserName(danhMucSoBanNganh.getUserName());
        danhMucSoBanNganhImpl.setCreateDate(danhMucSoBanNganh.getCreateDate());
        danhMucSoBanNganhImpl.setModifiedDate(danhMucSoBanNganh.getModifiedDate());
        danhMucSoBanNganhImpl.setTenSoBanNganh(danhMucSoBanNganh.getTenSoBanNganh());
        danhMucSoBanNganhImpl.setDiaChi(danhMucSoBanNganh.getDiaChi());
        danhMucSoBanNganhImpl.setSoDienThoai(danhMucSoBanNganh.getSoDienThoai());
        danhMucSoBanNganhImpl.setFax(danhMucSoBanNganh.getFax());
        danhMucSoBanNganhImpl.setEmail(danhMucSoBanNganh.getEmail());
        danhMucSoBanNganhImpl.setNguoiLienHe(danhMucSoBanNganh.getNguoiLienHe());
        danhMucSoBanNganhImpl.setWebsite(danhMucSoBanNganh.getWebsite());
        danhMucSoBanNganhImpl.setChuThich(danhMucSoBanNganh.getChuThich());
        danhMucSoBanNganhImpl.setDeleted(danhMucSoBanNganh.getDeleted());

        return danhMucSoBanNganhImpl;
    }

    /**
     * Returns the danh muc so ban nganh with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
     *
     * @param primaryKey the primary key of the danh muc so ban nganh
     * @return the danh muc so ban nganh
     * @throws qldc.pm.qlktcsdldc.NoSuchDanhMucSoBanNganhException if a danh muc so ban nganh with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DanhMucSoBanNganh findByPrimaryKey(Serializable primaryKey)
        throws NoSuchDanhMucSoBanNganhException, SystemException {
        DanhMucSoBanNganh danhMucSoBanNganh = fetchByPrimaryKey(primaryKey);

        if (danhMucSoBanNganh == null) {
            if (_log.isWarnEnabled()) {
                _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
            }

            throw new NoSuchDanhMucSoBanNganhException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
                primaryKey);
        }

        return danhMucSoBanNganh;
    }

    /**
     * Returns the danh muc so ban nganh with the primary key or throws a {@link qldc.pm.qlktcsdldc.NoSuchDanhMucSoBanNganhException} if it could not be found.
     *
     * @param IDQLKT_DanhMucSoBanNganh the primary key of the danh muc so ban nganh
     * @return the danh muc so ban nganh
     * @throws qldc.pm.qlktcsdldc.NoSuchDanhMucSoBanNganhException if a danh muc so ban nganh with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DanhMucSoBanNganh findByPrimaryKey(long IDQLKT_DanhMucSoBanNganh)
        throws NoSuchDanhMucSoBanNganhException, SystemException {
        return findByPrimaryKey((Serializable) IDQLKT_DanhMucSoBanNganh);
    }

    /**
     * Returns the danh muc so ban nganh with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param primaryKey the primary key of the danh muc so ban nganh
     * @return the danh muc so ban nganh, or <code>null</code> if a danh muc so ban nganh with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DanhMucSoBanNganh fetchByPrimaryKey(Serializable primaryKey)
        throws SystemException {
        DanhMucSoBanNganh danhMucSoBanNganh = (DanhMucSoBanNganh) EntityCacheUtil.getResult(DanhMucSoBanNganhModelImpl.ENTITY_CACHE_ENABLED,
                DanhMucSoBanNganhImpl.class, primaryKey);

        if (danhMucSoBanNganh == _nullDanhMucSoBanNganh) {
            return null;
        }

        if (danhMucSoBanNganh == null) {
            Session session = null;

            try {
                session = openSession();

                danhMucSoBanNganh = (DanhMucSoBanNganh) session.get(DanhMucSoBanNganhImpl.class,
                        primaryKey);

                if (danhMucSoBanNganh != null) {
                    cacheResult(danhMucSoBanNganh);
                } else {
                    EntityCacheUtil.putResult(DanhMucSoBanNganhModelImpl.ENTITY_CACHE_ENABLED,
                        DanhMucSoBanNganhImpl.class, primaryKey,
                        _nullDanhMucSoBanNganh);
                }
            } catch (Exception e) {
                EntityCacheUtil.removeResult(DanhMucSoBanNganhModelImpl.ENTITY_CACHE_ENABLED,
                    DanhMucSoBanNganhImpl.class, primaryKey);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return danhMucSoBanNganh;
    }

    /**
     * Returns the danh muc so ban nganh with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param IDQLKT_DanhMucSoBanNganh the primary key of the danh muc so ban nganh
     * @return the danh muc so ban nganh, or <code>null</code> if a danh muc so ban nganh with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public DanhMucSoBanNganh fetchByPrimaryKey(long IDQLKT_DanhMucSoBanNganh)
        throws SystemException {
        return fetchByPrimaryKey((Serializable) IDQLKT_DanhMucSoBanNganh);
    }

    /**
     * Returns all the danh muc so ban nganhs.
     *
     * @return the danh muc so ban nganhs
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<DanhMucSoBanNganh> findAll() throws SystemException {
        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the danh muc so ban nganhs.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.qlktcsdldc.model.impl.DanhMucSoBanNganhModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of danh muc so ban nganhs
     * @param end the upper bound of the range of danh muc so ban nganhs (not inclusive)
     * @return the range of danh muc so ban nganhs
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<DanhMucSoBanNganh> findAll(int start, int end)
        throws SystemException {
        return findAll(start, end, null);
    }

    /**
     * Returns an ordered range of all the danh muc so ban nganhs.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.qlktcsdldc.model.impl.DanhMucSoBanNganhModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of danh muc so ban nganhs
     * @param end the upper bound of the range of danh muc so ban nganhs (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of danh muc so ban nganhs
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<DanhMucSoBanNganh> findAll(int start, int end,
        OrderByComparator orderByComparator) throws SystemException {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
                (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
            finderArgs = FINDER_ARGS_EMPTY;
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
            finderArgs = new Object[] { start, end, orderByComparator };
        }

        List<DanhMucSoBanNganh> list = (List<DanhMucSoBanNganh>) FinderCacheUtil.getResult(finderPath,
                finderArgs, this);

        if (list == null) {
            StringBundler query = null;
            String sql = null;

            if (orderByComparator != null) {
                query = new StringBundler(2 +
                        (orderByComparator.getOrderByFields().length * 3));

                query.append(_SQL_SELECT_DANHMUCSOBANNGANH);

                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
                    orderByComparator);

                sql = query.toString();
            } else {
                sql = _SQL_SELECT_DANHMUCSOBANNGANH;

                if (pagination) {
                    sql = sql.concat(DanhMucSoBanNganhModelImpl.ORDER_BY_JPQL);
                }
            }

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                if (!pagination) {
                    list = (List<DanhMucSoBanNganh>) QueryUtil.list(q,
                            getDialect(), start, end, false);

                    Collections.sort(list);

                    list = new UnmodifiableList<DanhMucSoBanNganh>(list);
                } else {
                    list = (List<DanhMucSoBanNganh>) QueryUtil.list(q,
                            getDialect(), start, end);
                }

                cacheResult(list);

                FinderCacheUtil.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Removes all the danh muc so ban nganhs from the database.
     *
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeAll() throws SystemException {
        for (DanhMucSoBanNganh danhMucSoBanNganh : findAll()) {
            remove(danhMucSoBanNganh);
        }
    }

    /**
     * Returns the number of danh muc so ban nganhs.
     *
     * @return the number of danh muc so ban nganhs
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countAll() throws SystemException {
        Long count = (Long) FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
                FINDER_ARGS_EMPTY, this);

        if (count == null) {
            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(_SQL_COUNT_DANHMUCSOBANNGANH);

                count = (Long) q.uniqueResult();

                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
                    FINDER_ARGS_EMPTY, count);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
                    FINDER_ARGS_EMPTY);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    /**
     * Initializes the danh muc so ban nganh persistence.
     */
    public void afterPropertiesSet() {
        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
                    com.liferay.util.service.ServiceProps.get(
                        "value.object.listener.qldc.pm.qlktcsdldc.model.DanhMucSoBanNganh")));

        if (listenerClassNames.length > 0) {
            try {
                List<ModelListener<DanhMucSoBanNganh>> listenersList = new ArrayList<ModelListener<DanhMucSoBanNganh>>();

                for (String listenerClassName : listenerClassNames) {
                    listenersList.add((ModelListener<DanhMucSoBanNganh>) InstanceFactory.newInstance(
                            getClassLoader(), listenerClassName));
                }

                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
            } catch (Exception e) {
                _log.error(e);
            }
        }
    }

    public void destroy() {
        EntityCacheUtil.removeCache(DanhMucSoBanNganhImpl.class.getName());
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }
}
